<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
                <span>是否导航：</span>
                <el-select v-model="findform.nav" placeholder="是否导航" class="handle-select mr10">
                    <el-option key="1" label="是" value="y"></el-option>
                    <el-option key="2" label="否" value="n"></el-option>
                </el-select>
                <span>状态：</span>
                <el-select v-model="findform.status" placeholder="状态" class="handle-select mr10">
                    <el-option key="1" label="正常" value="y"></el-option>
                    <el-option key="2" label="锁定" value="n"></el-option>
                </el-select>
                <el-input v-model="findform.name" placeholder="游戏类名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-refresh" @click="back">刷新</el-button>
                <el-button type="primary" icon="el-icon-circle-plus-outline" @click="add">添加</el-button>
            </div>
            <!-- 分割线 -->
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="name" label="类别名称" align="center"></el-table-column>
                <el-table-column label="图片" align="center">
                    <template slot-scope="scope">
                        <el-image class="table-td-thumb" style="border-radius: 50%" @click="showscope(scope)" :src="scope.row.img">
                        </el-image>
                    </template>
                </el-table-column>
                <el-table-column label="是否导航" align="center">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.nav === 'y'" type="success">是</el-tag>
                        <el-tag v-else type="danger">否</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.status === 'y'" type="success">正常</el-tag>
                        <el-tag v-else type="danger">锁定</el-tag>
                    </template>
                </el-table-column>

                <el-table-column prop="orderNumber" label="序号" align="center"> </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-lock" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >锁定</el-button
                        >
                        <el-button type="text" icon="el-icon-lx-roundcheck" class="green" @click="handleunDelete(scope.$index, scope.row)"
                            >解锁</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分割线 -->
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="游戏类名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="序号">
                    <el-input v-model="form.orderNumber"></el-input>
                </el-form-item>
                <el-form-item label=" 是否导航">
                    <template>
                        <el-select v-model="form.nav" placeholder="是否导航">
                            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                        </el-select>
                    </template>
                </el-form-item>
                <el-form-item label=" 状态选择">
                    <template>
                        <el-select v-model="form.status" placeholder="选择状态">
                            <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                        </el-select>
                    </template>
                </el-form-item>
                <el-form-item label=" 图片选择">
                    <el-input v-model="form.img" :disabled="true"></el-input>
                    <!-- 图片上传 -->
                    <el-upload
                        class="upload-demo"
                        action="http://localhost:8080/upload"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :on-exceed="overlimit"
                        :on-success="success"
                        :file-list="fileList"
                        :limit="uploadnum"
                        list-type="picture"
                    >
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancle">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 新增弹出框 -->
        <el-dialog title="编辑" :visible.sync="addVisible" width="30%">
            <el-form ref="form" :model="addform" label-width="70px">
                <el-form-item label="游戏类名">
                    <el-input v-model="addform.name" @blur="checkAddName"></el-input>
                </el-form-item>
                <el-form-item label="序号">
                    <el-input v-model="addform.orderNumber" @blur="checkAddName"></el-input>
                </el-form-item>
                <el-form-item label="是否导航">
                    <template>
                        <el-select v-model="addform.nav" placeholder="是否导航">
                            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                        </el-select>
                    </template>
                </el-form-item>
                <el-form-item label=" 状态选择">
                    <template>
                        <el-select v-model="addform.status" placeholder="选择状态">
                            <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                        </el-select>
                    </template>
                </el-form-item>
                <el-form-item label=" 图片选择">
                    <el-input v-model="addform.img" :disabled="true"></el-input>
                    <!-- 图片上传 -->
                    <el-upload
                        class="upload-demo"
                        action="http://localhost:8080/upload"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :on-exceed="overlimit"
                        :on-success="success"
                        :file-list="fileList"
                        :limit="uploadnum"
                        list-type="picture"
                    >
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancleAdd">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { fetchData } from '../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            tempimg: '',
            fileList: [],
            uploadnum: 1,
            options: [
                {
                    value: 'y',
                    label: '是'
                },
                {
                    value: 'n',
                    label: '否'
                }
            ],
            statusOptions: [
                {
                    value: 'y',
                    label: '正常'
                },
                {
                    value: 'n',
                    label: '锁定'
                }
            ],
            value: '',
            query: {
                address: '',
                name: '',
                pageIndex: 1,
                pageSize: 5
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            addVisible: false,
            pageTotal: 0,
            // form就是goodscategory
            form: {
                name: '',
                nav: '',
                orderNumber: 0,
                status: '',
                img: ''
            },
            findform: {
                name: '',
                nav: 'y',
                orderNumber: 0,
                status: 'y',
                img: ''
            },
            addform: {
                name: '',
                nav: '',
                orderNumber: 0,
                status: '',
                img: ''
            },
            formbf: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
        this.$axios.get('admin/category/show/1/5').then((r) => {
            this.tableData = r.data.data.data;
            this.query.pageIndex = r.data.data.currPage;
            this.query.pageSize = r.data.data.pageSize;
            this.pageTotal = r.data.data.totalNums;
        });
    },
    methods: {
        //文件上传成功返回
        success(response) {
            console.log(response);
            this.tempimg = response.data;
        },
        //选择了多张图片时报警
        overlimit() {
            this.$message.warning(`一次只能选择一张图片，或者删除已有的图片再进行选择`);
        },
        //图片上传
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handlePreview(file) {
            console.log(file);
        },
        showscope(scope) {
            console.log(scope);
        },
        //新增类别失焦检查
        checkAddName() {
            this.$axios.post('admin/category/checkName', this.addform).then((r) => {
                if (r.data.code != 200) {
                    this.$message.warning(`游戏类别名或序号已存在`);
                }
            });
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            fetchData(this.query).then((res) => {
                console.log(res);
                this.tableData = res.list;
                this.pageTotal = res.pageTotal || 50;
            });
        },
        // 搜索
        handleSearch() {
            this.$axios.post('admin/category/fuzzy', this.findform).then((r) => {
                this.tableData = r.data.data;
                console.log(r.data.data);
            });
        },
        //取消搜索返回
        back() {
            this.$axios.get('admin/category/show/1/5').then((r) => {
                this.tableData = r.data.data.data;
                this.query.pageIndex = r.data.data.currPage;
                this.query.pageSize = r.data.data.pageSize;
                this.pageTotal = r.data.data.totalNums;
            });
        },
        // 锁定
        handleDelete(index, row) {
            row.status = 'n';
            this.$axios.get('admin/category/lock/' + row.id).then((r) => {
                this.$message.success('锁定成功');
            });
        },
        //解锁
        handleunDelete(index, row) {
            row.status = 'y';
            this.$axios.get('admin/category/unlock/' + row.id).then((r) => {
                this.$message.success('解锁成功');
            });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log(val);
        },
        //批量删除
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
                this.$axios.get('admin/category/del/' + this.multipleSelection[i].id).then((r) => {});
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
            this.$axios.get('admin/category/show/1/5').then((r) => {
                this.tableData = r.data.data.data;
                this.query.pageIndex = r.data.data.currPage;
                this.query.pageSize = r.data.data.pageSize;
                this.pageTotal = r.data.data.totalNums;
            });
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
            this.formbf = JSON.parse(JSON.stringify(row));
        },
        // 确认编辑
        saveEdit() {
            this.editVisible = false;
            this.form.img = this.tempimg;
            this.$axios.post('/admin/category/update', this.form).then((r) => {
                if (r.data.code == 200) {
                    this.$axios.get('admin/category/show/1/5').then((r) => {
                        this.tableData = r.data.data.data;
                        this.query.pageIndex = r.data.data.currPage;
                        this.query.pageSize = r.data.data.pageSize;
                        this.pageTotal = r.data.data.totalNums;
                        this.fileList = [];
                    });
                    this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                } else {
                    this.form.name = this.formbf.name;
                    this.form.orderNumber = this.formbf.orderNumber;
                    this.form.nav = this.formbf.nav;
                    this.form.status = this.formbf.status;
                    this.$message.warning(`游戏类别名已存在，请重新修改`);
                    this.fileList = [];
                }
            });
        },
        //取消编辑
        cancle() {
            this.editVisible = false;
            this.form.name = this.formbf.name;
            this.form.orderNumber = this.formbf.orderNumber;
            this.form.nav = this.formbf.nav;
            this.form.status = this.formbf.status;
            this.fileList = [];
        },
        // 分页导航
        handlePageChange(val) {
            this.$axios.get('admin/category/show/' + val + '/5').then((r) => {
                this.tableData = r.data.data.data;
                this.query.pageIndex = r.data.data.currPage;
                this.query.pageSize = r.data.data.pageSize;
                this.pageTotal = r.data.data.totalNums;
            });
        },
        //添加
        add() {
            this.addVisible = true;
        },
        //确认添加
        saveAdd() {
            this.addform.img = this.tempimg;
            this.$axios.post('admin/category/addCategory', this.addform).then((r) => {
                if (r.data.code == 500) {
                    this.$message.warning(`序号已存在`);
                } else {
                    this.$axios.get('admin/category/show/1/5').then((r) => {
                        this.tableData = r.data.data.data;
                        this.query.pageIndex = r.data.data.currPage;
                        this.query.pageSize = r.data.data.pageSize;
                        this.pageTotal = r.data.data.totalNums;
                    });
                }
            });
            this.addVisible = false;
            this.fileList = [];
        },
        //取消添加
        cancleAdd() {
            this.addVisible = false;
            this.addform = {};
            this.fileList = [];
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
